package vmcs.customer;

import java.util.ArrayList;
import java.util.List;

import vmcs.common.VMCSException;
import vmcs.controller.TransactionController;
import vmcs.customer.coinstrategy.CompositeCoinValidationStrategy;
import vmcs.customer.coinstrategy.DiameterCoinValidationStrategy;
import vmcs.customer.coinstrategy.ICoinValidationStrategy;
import vmcs.customer.coinstrategy.WeightCoinValidationStrategy;
import vmcs.store.CashStore;
import vmcs.store.Coin;
import vmcs.store.Store;
import vmcs.store.StoreController;
import vmcs.store.StoreItem;

public class CoinReceiver {

	private TransactionController transactionController;
	private List<Coin> coins;
	private int totalInserted;

	public CoinReceiver(TransactionController transactionController) {
		this.transactionController = transactionController;
		this.coins = new ArrayList<Coin>();
	}

	public void startReceive() {
		transactionController.getCustomerPanel().setCoinInputActive(true);	
		transactionController.getCustomerPanel().setMoneyInsertedValue("0" + " C");
		totalInserted = 0;
		coins.clear();
	}

	public void receiveCoin(double weight) throws VMCSException {

		Coin coin = new Coin();
		coin.setWeight(weight);
		transactionController.getCustomerPanel().setInvalidCoinState(false);
		transactionController.getCustomerPanel().setCollectCoinValue("0" + " C");
		transactionController.getCustomerPanel().setCoinInputActive(false);
		//validation
		CashStore cashStore = (CashStore)transactionController.getMainController().getStoreController().getStore(Store.CASH);
		CompositeCoinValidationStrategy compostieStrategy = new CompositeCoinValidationStrategy();
		ICoinValidationStrategy weightStrategy = new WeightCoinValidationStrategy();
		ICoinValidationStrategy diameterStrategy = new DiameterCoinValidationStrategy();
		compostieStrategy.add(weightStrategy);
		compostieStrategy.add(diameterStrategy);
		cashStore.setStrategy(compostieStrategy);
		Coin coinInserted=null;
		try{
			coinInserted=cashStore.findCoin(coin);
		}catch(Exception e)
		{
			throw new VMCSException();
		}
		if(coinInserted == null){
			transactionController.getCustomerPanel().setInvalidCoinState(true);
			transactionController.getCustomerPanel().setCollectCoinValue("Invalid Coin");
			transactionController.getCustomerPanel().setCoinInputActive(true);
		}else{
			coins.add(coinInserted);
			totalInserted += coinInserted.getValue();
			transactionController.getCustomerPanel().setMoneyInsertedValue(String.valueOf(totalInserted) + " C");
			transactionController.insertCoin(coinInserted.getValue());
		}
	}

	public void continueReceive() {
		transactionController.getCustomerPanel().setCoinInputActive(true);
	}

	public boolean storeCash() throws VMCSException {
		for(Coin coin : coins){
			try {
				transactionController.getMainController().getMachineryController().storeCoin(coin);
			} catch (VMCSException e) {
				throw new VMCSException("Storing Coins",e.getMessage());
			}
		}

		this.coins.clear();
		this.totalInserted = 0;
		transactionController.getCustomerPanel().setMoneyInsertedValue("0" + " C");


		return true;
	}

	public void stopReceive() {
		transactionController.getCustomerPanel().setCoinInputActive(false);
	}

	public void refundCash() {
		if(totalInserted == 0)return;
		transactionController.getCustomerPanel().setCollectCoinValue(String.valueOf(totalInserted) + " C");
		transactionController.getCustomerPanel().setMoneyInsertedValue("0" + " C");
		transactionController.getCustomerPanel().setInvalidCoinState(false);
	}

	public boolean setActive() {
	
		return false;
	}

	public StoreItem[] getCashStore() {
		StoreItem[] items = transactionController.getMainController().getStoreController().getStoreItems(Store.CASH);
		return items;
	}

	public TransactionController getTransactionController() {
		return transactionController;
	}



}
